﻿using System.IO;
using System.Reflection;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Web.Mvc.Html;
using System.ComponentModel;
using System.Linq.Expressions;

namespace System.Web.Mvc
{
    public static class CreateViewBuilder
    {
        public static MvcHtmlString CreateView<TModel>(this HtmlHelper<TModel> htmlHelper, string[] columns = null)
        {
            StringBuilder builder = new StringBuilder();
            StringBuilder itemsBuilder = new StringBuilder();

            Guid id = Guid.Parse(typeof(TModel).GetProperty("Id").GetValue(htmlHelper.ViewData.Model, null).ToString());

            foreach (var pInfo in typeof(TModel).GetProperties())
            {

                string editor = "";
                if (pInfo.PropertyType.IsEnum)
                {
                    editor = htmlHelper.EnumDropDownListFor(m => pInfo).ToHtmlString();
                }

                itemsBuilder.AppendFormat(
@"<div class=""editor-label"">
            {0}
        </div>
        <div class=""editor-field"">
            {1}
        </div>{2}", htmlHelper.Label(pInfo.Name), htmlHelper.Editor(pInfo.Name), Environment.NewLine + Environment.NewLine);
            }

            string beginForm = htmlHelper.BeginForm("Create", htmlHelper.ViewContext.RouteData.Values["Controller"].ToString(), new { Id = id }, FormMethod.Post).ToString();

            builder.AppendFormat(
@"{0}
    <fieldset>
        <legend>{1}</legend>
        {2}
    </fieldset>

<p>
    <input type=""submit"" value=""Save"" class=""button"" />
</p>

</form> ", htmlHelper.ValidationSummary(true), typeof(TModel).Name, itemsBuilder);

            return MvcHtmlString.Create(builder.ToString());
        }


        public static MvcHtmlString EditView<TModel>(this HtmlHelper<TModel> htmlHelper, string[] columns = null)
        {
            StringBuilder builder = new StringBuilder();
            StringBuilder itemsBuilder = new StringBuilder();

            Guid id = Guid.Parse(typeof(TModel).GetProperty("Id").GetValue(htmlHelper.ViewData.Model, null).ToString());

            foreach (var pInfo in typeof(TModel).GetProperties())
            {
                if (pInfo.PropertyType.IsEnum)
                {
                    htmlHelper.EnumDropDownListFor(pInfo, null);
                    itemsBuilder.AppendFormat(
       @"<div class=""editor-label"">
            {0}
        </div>
        <div class=""editor-field"">
            {1}
        </div>{2}",
                        htmlHelper.Label(pInfo.Name), htmlHelper.ViewData["enumValues"],
                        Environment.NewLine + Environment.NewLine);
                }
                else
                {
                    itemsBuilder.AppendFormat(
            @"<div class=""editor-label"">
            {0}
        </div>
        <div class=""editor-field"">
            {1}
        </div>{2}",
                        htmlHelper.Label(pInfo.Name), htmlHelper.Editor(pInfo.Name),
                        Environment.NewLine + Environment.NewLine);
                }
            }

            string beginForm = htmlHelper.BeginForm("Edit", htmlHelper.ViewContext.RouteData.Values["Controller"].ToString(), new { Id = id }, FormMethod.Post).ToString();

            builder.AppendFormat(
@"{0}
    <fieldset>
        <legend>{1}</legend>
        {2}
    </fieldset>

<p>
    <input type=""submit"" value=""Save"" class=""button"" />
</p>

</form> ", htmlHelper.ValidationSummary(true), typeof(TModel).Name, itemsBuilder);

            return MvcHtmlString.Create(builder.ToString());
        }
    }
}
